package com.zzl.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Title: RabbitMQConfig.class<br>
 * Description: <br>
 * Copyright (c)  2021    <br>
 * Create DateTime: 2021/7/15 16:54 <br>
 *
 * @author zhuangzhuangliang
 */
@Configuration
public class RabbitMqConfig {

    //普通队列
    public static final String X_EXCHANGE = "X";
    public static final String QA = "QA";
    public static final String QB = "QB";
    public static final String QC = "QC";

    //死信队列 交换机
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";
    public static final String DEAD_LETTER_QUEUE = "QD";

    /**
     *
     * @return
     */
    @Bean("xExchange")
    public DirectExchange xExchange() {
        return new DirectExchange(X_EXCHANGE);
    }

    @Bean("yExchange")
    public DirectExchange yExchange() {
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    /**
     * 普通队列 routingKey
     * @return
     */
    @Bean("queueA")
    public Queue queueA(){

        return QueueBuilder.nonDurable(QA)
                .deadLetterExchange(Y_DEAD_LETTER_EXCHANGE)
                .deadLetterRoutingKey("YD")
                .ttl(10000)
                .build();
    }

    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA, @Qualifier("xExchange") Exchange exchangeX){
        return BindingBuilder.bind(queueA).to(exchangeX).with("XA").noargs();
    }

    @Bean("queueB")
    public Queue queueB(){

        return QueueBuilder.nonDurable(QB)
                .deadLetterExchange(Y_DEAD_LETTER_EXCHANGE)
                .deadLetterRoutingKey("YD")
                .ttl(40000)
                .build();
    }

    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueA, @Qualifier("xExchange") Exchange exchangeX){
        return BindingBuilder.bind(queueA).to(exchangeX).with("XB").noargs();
    }

    @Bean("queueC")
    public Queue queueC(){

        return QueueBuilder.nonDurable(QC)
                .deadLetterExchange(Y_DEAD_LETTER_EXCHANGE)
                .deadLetterRoutingKey("YD")
                .build();
    }

    @Bean
    public Binding queueBBindingC(@Qualifier("queueC") Queue queueA, @Qualifier("xExchange") Exchange exchangeX){
        return BindingBuilder.bind(queueA).to(exchangeX).with("XC").noargs();
    }

    /**
     * 死信队列和routingKey
     * @return
     */
    @Bean("queueDead")
    public Queue queueDead(){
        return QueueBuilder.nonDurable(DEAD_LETTER_QUEUE).build();
    }

    @Bean
    public Binding queueDeadBindingY(@Qualifier("queueDead") Queue queueDead, @Qualifier("yExchange") Exchange exchangeY){
        return BindingBuilder.bind(queueDead).to(exchangeY).with("YD").noargs();
    }
}
